package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.ExamManage;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.ExamManageServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "试卷接口")
@RestController
public class ExamManageController {

    @Autowired
    private ExamManageServiceImpl examManageService;

    @ApiOperation(value = "查询所有试卷", notes = "查询所有试卷", httpMethod = "GET")
    @GetMapping("/exams")
    public R findAll() {
        return R.ok().data(examManageService.findAll());
    }

    @ApiOperation(value = "分页查询试卷", notes = "分页查询试卷", httpMethod = "GET")
    @GetMapping("/exams/{page}/{size}")
    public R findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<ExamManage> examManage = new Page<>(page, size);
        IPage<ExamManage> all = examManageService.findPage(examManage);
        return R.ok().data(all);
    }

    @ApiOperation(value = "分页查询试卷", notes = "分页查询试卷", httpMethod = "GET")
    @GetMapping("/exams/{page}/{size}/{description}/{collegeId}/{majorId}")
    public R findAllBy(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("description") String description, @PathVariable("collegeId") Integer collegeId, @PathVariable("majorId") Integer majorId) {
        Page<ExamManage> examManage = new Page<>(page, size);
        IPage<ExamManage> all = examManageService.findPage(examManage, description, collegeId, majorId);
        return R.ok().data(all);
    }

    @ApiOperation(value = "根据id查询试卷信息", notes = "根据id查询试卷信息", httpMethod = "GET")
    @GetMapping("/exam/{examCode}")
    public R<ExamManage> findById(@PathVariable("examCode") Integer examCode) {
        ExamManage res = examManageService.findById(examCode);
        if (res == null) {
            return R.logicError("考试编号不存在");
        }
        return R.<ExamManage>ok().data(res);
    }

    @ApiOperation(value = "根据id删除试卷信息", notes = "根据id删除试卷信息", httpMethod = "DELETE")
    @DeleteMapping("/exam/{examCode}")
    public R deleteById(@PathVariable("examCode") Integer examCode) {
        examManageService.delete(examCode);
        return R.ok("删除成功").data("删除成功");
    }

    @ApiOperation(value = "更新试卷信息", notes = "更新试卷信息", httpMethod = "PUT")
    @PutMapping("/exam")
    public R update(@RequestBody ExamManage exammanage) {
        int res = examManageService.update(exammanage);
        return R.ok("更新成功").data(res);
    }

    @ApiOperation(value = "添加试卷信息", notes = "添加试卷信息", httpMethod = "POST")
    @PostMapping("/exam")
    public R add(@RequestBody ExamManage exammanage) {
        int res = examManageService.add(exammanage);
        if (res == 1) {
            return R.ok("添加成功").data(res);
        } else {
            return R.ok("添加失败").data(res);
        }
    }

    @ApiOperation(value = "获取最大试卷编号", notes = "获取最大试卷编号", httpMethod = "GET")
    @GetMapping("/examManagePaperId")
    public R findOnlyPaperId() {
        ExamManage res = examManageService.findOnlyPaperId();
        if (res != null) {
            return R.ok("请求成功").data(res);
        }
        return R.logicError("请求失败");
    }
}
